home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Magazine / Online / QMail / source / remoteinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-15  |  1.6 KB  |  66 lines

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <fcntl.h>
  5. #include "byte.h"
  6. #include "substdio.h"
  7. #include "ip.h"
  8. #include "fmt.h"
  9. #include "timeoutconn.h"
  10. #include "timeoutread.h"
  11. #include "timeoutwrite.h"
  12. #include "remoteinfo.h"
  13.  
  14. static char line[999];
  15.  
  16. char *remoteinfo_get(ipr,rp,ipl,lp,timeout)
  17. struct ip_address *ipr;
  18. unsigned long rp;
  19. struct ip_address *ipl;
  20. unsigned long lp;
  21. int timeout;
  22. {
  23.   char *x;
  24.   int s;
  25.   struct sockaddr_in sin;
  26.   substdio ss;
  27.   char buf[32];
  28.   unsigned int len;
  29.   int numcolons;
  30.   char ch;
  31.  
  32.   s = socket(AF_INET,SOCK_STREAM,0);
  33.   if (s == -1) return 0;
  34.  
  35.   byte_zero(&sin,sizeof(sin));
  36.   sin.sin_family = AF_INET;
  37.   byte_copy(&sin.sin_addr,4,ipl);
  38.   sin.sin_port = 0;
  39.   if (bind(s,(struct sockaddr *) &sin,sizeof(sin)) == -1) { close(s); return 0; }
  40.   if (timeoutconn(s,ipr,113,timeout) == -1) { close(s); return 0; }
  41.   fcntl(s,F_SETFL,fcntl(s,F_GETFL,0) & ~O_NDELAY);
  42.  
  43.   len = 0;
  44.   len += fmt_ulong(line + len,rp);
  45.   len += fmt_str(line + len," , ");
  46.   len += fmt_ulong(line + len,lp);
  47.   len += fmt_str(line + len,"\r\n");
  48.  
  49.   substdio_fdbuf(&ss,timeoutwrite,TIMEOUTWRITE(timeout,s),buf,sizeof(buf));
  50.   if (substdio_putflush(&ss,line,len) == -1) { close(s); return 0; }
  51.  
  52.   substdio_fdbuf(&ss,timeoutread,TIMEOUTREAD(timeout,s),buf,sizeof(buf));
  53.   x = line;
  54.   numcolons = 0;
  55.   for (;;) {
  56.     if (substdio_get(&ss,&ch,1) != 1) { close(s); return 0; }
  57.     if ((ch == ' ') || (ch == '\t') || (ch == '\r')) continue;
  58.     if (ch == '\n') break;
  59.     if (numcolons < 3) { if (ch == ':') ++numcolons; }
  60.     else { *x++ = ch; if (x == line + sizeof(line) - 1) break; }
  61.   }
  62.   *x = 0;
  63.   close(s);
  64.   return line;
  65. }
  66.